using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TeamTime.Classes;
using StillVoiceCommon;
using TeamTime.Classes;
namespace TeamTime
{
    public partial class StaffMembersForm : System.Web.UI.Page //TeamTime.Classes.CompressedPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Companies companies = new Companies();
                StaffMembers staffMembers = new StaffMembers();
                //Functions.dropDownListPopulate(this.ddlDesignationID, sqlDB.getDataSet("SELECT ID,Description FROM Designation ORDER BY Description ASC"), "ID", "Description");
                Functions.dropDownListPopulate(this.ddlOfficeID, sqlDB.getDataSet("SELECT ID,Description FROM Office ORDER BY Description ASC"), "ID", "Description");
                //Functions.dropDownListPopulate(this.ddlPersonID, sqlDB.getDataSet("SELECT ID,FullName FROM ViewPersons ORDER BY FullName ASC"), "ID", "FullName");
                Functions.dropDownListPopulate(this.ddlDesignationID, sqlDB.getDataSet("SELECT ID,Description FROM Designation ORDER BY Description ASC"), "ID", "Description");
                ViewState["SortExpression"] = "FullName"; ViewState["SortDirection"]=  "asc";
                EnableControls();
                btnSearch_Click(sender, e);
                this.Form.DefaultFocus = this.txtSearchFor.ClientID;
                this.Form.DefaultButton = this.btnSearch.UniqueID;
            }

        }

        private void EnableControls()
        {
            Functions.enableChildControls(this.Form);
            this.trID.Visible = true;
            this.txtID.Enabled = false;
            this.txtUserNameModifiedBy.Enabled = false;
            this.txtRecordDate.Enabled = false;
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindGrid();
        }

        private void BindGrid()
        {
            StaffMembers staffMembers = new StaffMembers();
            this.GridView1.DataSource = staffMembers.searchStaffMembers(this.txtSearchFor.Text,
                ViewState["SortExpression"] + " " + ViewState["SortDirection"]);
            this.GridView1.DataBind();

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            //check for duplicates

            this.lnkLoadDup.Attributes["DupID"] = "";
            this.lnkLoadDup.Visible = false;

            Persons persons = new Persons();
            int personID = persons.GetPersonIDFromNameAndID(this.txtPersonName.Text);
            if (personID < 1) //No company found
            {
                this.reqValPersonName.IsValid = false;
                this.reqValPersonName.ErrorMessage = "* Person does not exist";
                popUp1.Show();
                return;
            }


            StaffMembers staffMembers = new StaffMembers();
            int dupID = staffMembers.getDuplicateStaffMemberID(personID, this.txtID.Text);

            if (dupID != 0)//Duplicate exists
            {
                this.lnkLoadDup.Attributes["DupID"] = dupID.ToString();
                this.lnkLoadDup.Visible = true;
                popUp1.Show();
            }
            else
            {
                StaffMember staffMember = new StaffMember();
                if (this.txtID.Text == "") { staffMember.setForAdd(); } else { staffMember.load(this.txtID.Text); }




                staffMember.Fields["PersonID"] = personID;

                //staffMember.Fields["PersonID"] = this.ddlPersonID.SelectedValue;
                staffMember.Fields["OfficeID"] = this.ddlOfficeID.SelectedValue;
                staffMember.Fields["DesignationID"] = this.ddlDesignationID.SelectedValue;
                //staffMember.Fields["Designation"] = this.txtDesignation.Text;
                staffMember.Fields["NeedsBusinessCards"] = this.chkNeedsBusinessCards.Checked;
                staffMember.Fields["StartDate"] = this.calStartDate.Text;
                if (Functions.IsDate(this.calEndDate.Text)) { staffMember.Fields["EndDate"] = this.calEndDate.Text; } else staffMember.Fields["EndDate"] = DBNull.Value;

                staffMember.UserIDModifiedBy = Convert.ToInt32(Session["UserID"]);
                staffMember.updateWithUserIDDate();

                //this.txtSearchFor.Text = staffMember.KeyValue.ToString();
                BindGrid();
                LoadDetailsForm(staffMember.KeyValue.ToString());
                staffMember = null;
                lblSaveMessage.ShowStatusMessage("Successfully saved");
                popUp1.Show();
            }
        }

        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            Functions.clearChildcontrols(this.pnlAdd);
            //this.ddlCompanyID.Enabled = true;
            popUp1.Show();
        }

        private void LoadDetailsForm(string ID)
        {
            //this.ddlCompanyID.Enabled = false;
            StaffMember staffMember = new StaffMember();
            staffMember.load(ID);
            Functions.populateFormControlsFromDataTableFields(staffMember, this.pnlAdd);
            this.txtPersonName.Text = staffMember.FullNameAndID;
            this.txtUserNameModifiedBy.Text = staffMember.UserNameModifiedBy;
        }

        protected void lnkLoadDup_Click(object sender, EventArgs e)
        {
            if (this.lnkLoadDup.Attributes["DupID"] != "")
            {
                LoadDetailsForm(this.lnkLoadDup.Attributes["DupID"]);
                this.lnkLoadDup.Visible = false;
            }
            popUp1.Show();
        }

        //Grid Methods

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                //                string id = ((LinkButton)e.CommandSource).CommandArgument;
                string id = e.CommandArgument.ToString();
                LoadDetailsForm(id);
                popUp1.Show();
            }
            else if (e.CommandName == "Delete")
            {
                StaffMember staffMember = new StaffMember();
                //string id = ((LinkButton)e.CommandSource).CommandArgument;
                string id = e.CommandArgument.ToString();
                staffMember.load(id);
                staffMember.delete();
                BindGrid();
            }

        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView theGrid = sender as GridView;  // refer to the GridView
            int newPageIndex = 0;

            if (-2 == e.NewPageIndex)
            { // when click the "GO" Button
                TextBox txtNewPageIndex = null;
                //GridViewRow pagerRow = theGrid.Controls[0].Controls[theGrid.Controls[0].Controls.Count - 1] as GridViewRow; // refer to PagerTemplate
                GridViewRow pagerRow = theGrid.BottomPagerRow; //


                if (null != pagerRow)
                {
                    txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;   // refer to the TextBox with the NewPageIndex value
                }

                if (null != txtNewPageIndex)
                {
                    newPageIndex = int.Parse(txtNewPageIndex.Text) - 1; // get the NewPageIndex
                }
            }
            else
            {  // when click the first, last, previous and next Button
                newPageIndex = e.NewPageIndex;
            }

            // check to prevent form the NewPageIndex out of the range
            newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
            newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;

            // specify the NewPageIndex
            theGrid.PageIndex = newPageIndex;

            // display a new page of data
            // GridView1.EditIndex = -1;
            //GridView1.PageIndex = e.NewPageIndex;
            BindGrid();
            // rebind the control
            // in this case of retrieving the data using the xxxDataSoucr control,
            // just do nothing, because the asp.net engine binds the data automatically
        }

       
        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            ViewState["SortDirection"]= Functions.getSortDirection(ViewState["SortExpression"].ToString(), 
                                                    e.SortExpression, ViewState["SortDirection"].ToString());
            ViewState["SortExpression"] = e.SortExpression;
            BindGrid();
        }


        //protected void GridView_Sort(object source, DataGridSortCommandEventArgs e)
        //{

        //    GridView1.Attributes.Add("SortExpression", e.SortExpression);
        //    BindGrid();
        //}

        //protected void GridView_Page(object source, DataGridPageChangedEventArgs e)
        //{
        //    // display a new page of data
        //    GridView1.EditItemIndex = -1;
        //    GridView1.CurrentPageIndex = e.NewPageIndex;
        //    BindGrid();
        //}


    }


}



